#include "Framebuffers.h"

mini_engine::Framebuffers *mini_engine::Framebuffers::m_framebuffers = nullptr;
vector<VkFramebuffer> mini_engine::Framebuffers::swapChainFramebuffers;

mini_engine::Framebuffers::Framebuffers(/* args */)
{
}

mini_engine::Framebuffers::~Framebuffers()
{
}

void mini_engine::Framebuffers::createFramebuffers()
{

    cout << "createFramebuffers" << endl;
    swapChainFramebuffers.resize(ImageViews::swapChainImageViews.size());

    for (size_t i = 0; i < ImageViews::swapChainImageViews.size(); i++)
    {
        VkImageView attachments[] = {
            ImageViews::swapChainImageViews[i]};

        VkFramebufferCreateInfo framebufferInfo{};
        framebufferInfo.sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO;
        framebufferInfo.renderPass = RenderPass::renderPass;
        framebufferInfo.attachmentCount = 1;
        framebufferInfo.pAttachments = attachments;
        framebufferInfo.width = SwapChain::swapChainExtent.width;
        framebufferInfo.height = SwapChain::swapChainExtent.height;
        framebufferInfo.layers = 1;

        if (vkCreateFramebuffer(LogicalDevice::logicalDevice, &framebufferInfo, nullptr, &swapChainFramebuffers[i]) != VK_SUCCESS)
        {
            throw std::runtime_error("failed to create framebuffer!");
        }
    }
}

mini_engine::Framebuffers *mini_engine::Framebuffers::getFramebuffers()
{
    if (m_framebuffers == nullptr)
    {
        m_framebuffers = new Framebuffers();
        FramebuffersDestruction framebuffersDestruction;
    }
    return m_framebuffers;
}

mini_engine::Framebuffers::FramebuffersDestruction::~FramebuffersDestruction()
{
    if (m_framebuffers != nullptr)
    {
        delete m_framebuffers;
        m_framebuffers = nullptr;
    }
}
